package utils

import "sync"

type GSyncMap struct {
	groupMap sync.Map
}

var (
	GlobalGSyncMap GSyncMap
)

func CheckGetGSyncMapGroup(groupid interface{}) *GSyncMapGroup {
	return GlobalGSyncMap.CheckGetGroup(groupid)
}

func RemoveFroupGlobalGSyncMap(groupid interface{}) {
	GlobalGSyncMap.RemoveGroup(groupid)
}

func (this *GSyncMap) FindGroup(groupid interface{}) *GSyncMapGroup {
	if val, loaded := this.groupMap.Load(groupid); loaded {
		return val.(*GSyncMapGroup)
	} else {
		return nil
	}
}

func (this *GSyncMap) RemoveGroup(groupid interface{}) {
	this.groupMap.Delete(groupid)
}

func (this *GSyncMap) CheckGetGroup(groupid interface{}) *GSyncMapGroup {
	if val, loaded := this.groupMap.Load(groupid); loaded {
		return val.(*GSyncMapGroup)
	} else {
		newRec := &GSyncMapGroup{}
		if val, loaded := this.groupMap.LoadOrStore(groupid, newRec); loaded {
			return val.(*GSyncMapGroup)
		} else {
			return newRec
		}
	}
}
